package com.sxu.greedy.day1;

import java.util.PriorityQueue;

/**
 * 将数组和减半的最小操作次数
 */
public class Test2 {
    public int halveArray(int[] nums) {
        // 构造大根堆
        PriorityQueue<Double> heap = new PriorityQueue<>((a,b)->b.compareTo(a));
        double sum = 0.0;
        for(int x : nums){
            heap.offer((double)x);
            sum += x;
        }
        sum /= 2.0;
        int count = 0;
        while(sum>0){
            double t = heap.poll() / 2.0;
            sum -= t;
            count++;
            heap.offer((double)t);
        }
        return count;
    }
}
